package egovframework.npqs.pqi.mobile.activity.disinfection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.R;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionAgreeItem;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionProgramItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsonparser.DisinfectionAgreeThread;
import egovframework.npqs.pqi.mobile.jsonparser.DisinfectionAgreeThread.DisinfectionAgreeThreadCallback;
import egovframework.npqs.pqi.mobile.jsonparser.DisinfectionProgramThread;
import egovframework.npqs.pqi.mobile.jsonparser.DisinfectionProgramThread.DisinfectionProgramThreadCallback;

//소독동의서, 계획서
public class DisinfectionAgreeActivity extends Activity implements 
	DisinfectionAgreeThreadCallback, DisinfectionProgramThreadCallback{
	private TextView titleTxt;	// 상단 타이틀
	private AppStatus myapp;
	private Context context;
	private String serchRecNo;
	private boolean value;
	private TextView openDateTxt, navigation;
	
	private TextView orgNmTxt, content_2_1, content_2_2;
	private TextView content_5_1, content_6_1;
	private TextView content_7_1, content_9_1, content_9_2;
	private TextView content_10_1, content_10_2, content_12_1;
	private TextView content_12_2, content_13_1, content_13_2, content_14_1;
	private TextView content_3_1, content_3_2, content_15_1, content_16_1, content_16_2;
	private TextView content_18_1, content_19_1;
	
	private DisinfectionAgreeThread disfectionThread;
	private DisinfectionProgramThread disfectionProgramThread;
	private DisinfectionAgreeItem disfectionItem;
	private DisinfectionProgramItem disfectionProgItem;
	
	private final static int MSG_QUARANTINE_COMPLETED = 0;
	private final static int MSG_QUARANTINE_ERROR = 1;
	private final static int MSG_NETWORK_ERROR = 2;
	
	private ImageButton btnHome, btn_exit;
	
	/* G-MOBILE 변수*/
    private String temp;
    
    private String mResult;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(value){
			AppStatus.navigation.addPathLocation("소독동의서");
		}else{
			AppStatus.navigation.addPathLocation("소독계획서");
		}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_program_agree);
		
		Intent intent = getIntent();
		value = intent.getBooleanExtra("value", false);
		serchRecNo = intent.getStringExtra("searchRecNo");
		
		init();
	}
	
	
	private void init(){
		//context = getApplicationContext();
		context = this;
		myapp = (AppStatus) getApplicationContext();
		myapp.startBroker(context);
		
		openDateTxt = (TextView)findViewById(R.id.open_date);
		titleTxt = (TextView)findViewById(R.id.headerTitleText);
		
		navigation = (TextView)findViewById(R.id.navigation);
		navigation.setText(AppStatus.navigation.getPath());
		Log.i("AppStatus.navigation.getPath()->", AppStatus.navigation.getPath());
		
		orgNmTxt = (TextView)findViewById(R.id.string_ynjybb);	// 검역지역본부
		content_2_1 = (TextView)findViewById(R.id.content_2_1);	// 검역신청번호
		content_2_2 = (TextView)findViewById(R.id.content_2_2);	// 접수일시
		content_5_1 = (TextView)findViewById(R.id.content_5_1);	// 상호
		content_6_1 = (TextView)findViewById(R.id.content_6_1);	// 성명
		content_7_1 = (TextView)findViewById(R.id.content_7_1);	// 주소
		content_9_1 = (TextView)findViewById(R.id.content_9_1);	// 수출입업자
		content_9_2 = (TextView)findViewById(R.id.content_9_2);	// 수출입국
		content_10_1 = (TextView)findViewById(R.id.content_10_1);	// 품명
		content_10_2 = (TextView)findViewById(R.id.content_10_2);	// 수량
		content_12_1 = (TextView)findViewById(R.id.content_12_1);	// 소독시설종류1
		content_12_2 = (TextView)findViewById(R.id.content_12_2);	// 투약일시1
		content_13_1 = (TextView)findViewById(R.id.content_13_1);	// 소독시설종류2
		content_13_2 = (TextView)findViewById(R.id.content_13_2);	// 투약일시2
		content_14_1 = (TextView)findViewById(R.id.content_14_1);	// 장소
		content_3_1 = (TextView)findViewById(R.id.content_3_1);	// 투약량
		content_3_2 = (TextView)findViewById(R.id.content_3_2);	// 소독처리규정
		content_15_1 = (TextView)findViewById(R.id.content_15_1);	// 약제명
		content_16_1 = (TextView)findViewById(R.id.content_16_1);	// 처리시간
		content_16_2 = (TextView)findViewById(R.id.content_16_2);	// 개방시간
		content_18_1 = (TextView)findViewById(R.id.content_18_1);	// 방제업체상호
		content_19_1 = (TextView)findViewById(R.id.content_19_1);	// 성명
		
		btn_exit = (ImageButton)findViewById(R.id.btn_exit);
		btn_exit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		btnHome = (ImageButton)findViewById(R.id.btnHome);
		btnHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myapp.setAbleHome(true);
				finish();
			}
		});

		myapp.showDialogNo(context);
		
		temp = "처음검색";
		//asyncTaskIsConnectedVPN();
		threadStart();
		
	}
	
	public void threadStart(){
    	if("처음검색".equals(temp)){
    		if(value){
    			titleTxt.setText("소독동의서");
    			openDateTxt.setVisibility(View.GONE);
    			disfectionThread = new DisinfectionAgreeThread(context, this
    					, myapp.getLoginItem().getOffCd(), serchRecNo, AppStatus.getmUserId());
    			disfectionThread.start();
    			
    		}else{
    			titleTxt.setText("소독계획서");
    			openDateTxt.setVisibility(View.VISIBLE);
    			disfectionProgramThread = new DisinfectionProgramThread(context, this
    					, myapp.getLoginItem().getOffCd(), serchRecNo, AppStatus.getmUserId() );
    			disfectionProgramThread.start();
    		}
    	}
	}
	
	private void layoutSetting(){
		if(!value){
			orgNmTxt.setText(disfectionProgItem.getAllInsttNm());
			content_2_1.setText(serchRecNo);   
			content_2_2.setText(disfectionProgItem.getRceptDate());
			content_5_1.setText(disfectionProgItem.getVenNm());
			content_6_1.setText(disfectionProgItem.getVenRep());
			content_7_1.setText(disfectionProgItem.getVenRnAdres() + disfectionProgItem.getVenRnAdresDetail());
			content_9_1.setText(disfectionProgItem.getTraDe());
			content_9_2.setText(disfectionProgItem.getNatNm());   
			content_10_1.setText(disfectionProgItem.getItmNm());
			content_10_2.setText(disfectionProgItem.getItmQty());
			content_12_1.setText(disfectionProgItem.getDikind1Nm().trim());
			content_12_2.setText(disfectionProgItem.getPreDe1() +" "+ disfectionProgItem.getPreTime());
			content_13_1.setText(disfectionProgItem.getDikind2Nm().trim());
			content_13_2.setText(disfectionProgItem.getPreDe2());
			content_14_1.setText(disfectionProgItem.getDiplace());
			content_3_1.setText(disfectionProgItem.getPreDe2());
			content_3_2.setText(disfectionProgItem.getMedQty());
			content_15_1.setText(disfectionProgItem.getMedNm());
			content_16_1.setText(disfectionProgItem.getJobTime() +" "+ disfectionProgItem.getJobMin());
			content_18_1.setText(disfectionProgItem.getSanghos());
			content_19_1.setText(disfectionProgItem.getNames());
			content_16_2.setText(disfectionProgItem.getOpenDe());
		}else{
			orgNmTxt.setText(disfectionItem.getAllInsttNm());
			content_2_1.setText(serchRecNo);   
			content_2_2.setText(disfectionItem.getRceptDate());
			content_5_1.setText(disfectionItem.getVenNm());
			content_6_1.setText(disfectionItem.getVenRep());
			content_7_1.setText(disfectionItem.getVenRnAdres() + disfectionItem.getVenRnAdresDetail());
			content_9_1.setText(disfectionItem.getTraDe());
			content_9_2.setText(disfectionItem.getNatNm());   
			content_10_1.setText(disfectionItem.getItmNm());
			content_10_2.setText(disfectionItem.getItmQty());
			content_12_1.setText(disfectionItem.getDikind1Nm().trim());
			content_12_2.setText(disfectionItem.getPreDe1() +" "+ disfectionItem.getPreTime());
			content_13_1.setText(disfectionItem.getDikind2Nm().trim());
			content_13_2.setText(disfectionItem.getPreDe2());
			content_14_1.setText(disfectionItem.getDiplace());
			content_3_1.setText(disfectionItem.getPreDe2());
			content_3_2.setText(disfectionItem.getMedQty());
			content_15_1.setText(disfectionItem.getMedNm());
			content_16_1.setText(disfectionItem.getJobTime() +" "+ disfectionItem.getJobMin());
			content_18_1.setText(disfectionItem.getSanghos());
			content_19_1.setText(disfectionItem.getNames());
		}
	}
	
	@Override
	public void OnDisinfectionAgreeCompleted(boolean isNetworkConntected,
			DisinfectionAgreeItem disfectionItem, String message) {
		Log.i("message-->", "message-->"+message);
		if(null != message && !"".equals(message)){
			mResult = message;
			reesultToast();
		}
		
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}
		
		if(!isNetworkConntected){	// 네트워크 접속이 되어 있지 않을 때
			Message msg = handler.obtainMessage();
			msg.what = MSG_NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		
		if(disfectionItem != null) {
			this.disfectionItem = disfectionItem;
			Message msg = handler.obtainMessage();
			msg.what = MSG_QUARANTINE_COMPLETED;
			handler.sendMessage(msg);
		}
		
	}

	@Override
	public void OnDisinfectionAgreeError(ErrorItem errorItem) {
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}

		Message msg = handler.obtainMessage();
		msg.what = MSG_QUARANTINE_ERROR;
		if(errorItem != null) {
			msg.obj = errorItem.getrDesc();
		}
		handler.sendMessage(msg);
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			switch (msg.what) {
			case MSG_QUARANTINE_COMPLETED:
				layoutSetting();
				break;

			case MSG_QUARANTINE_ERROR:
				String errMsg = "";
				if(msg == null ||msg.obj.toString().equals("")){
					errMsg = "서버와의 연결에 실패하였습니다.";
				}else{
					errMsg = msg.obj.toString();	
				}
				
				myapp.utilm.ShowDialog(DisinfectionAgreeActivity.this, "실패", errMsg, 20);
				
				break;

			case MSG_NETWORK_ERROR:
				myapp.utilm.ShowDialog(DisinfectionAgreeActivity.this, "실패", "네트워크에러", 20);

			default:
				break;
			}
		}
	};

	@Override
	public void OnDisinfectionProgramCompleted(boolean isNetworkConntected,
			DisinfectionProgramItem disfectionItem, String message) {
		Log.i("message-->", "message-->"+message);
		if(null != message && !"".equals(message)){
			mResult = message;
			reesultToast();
		}
		
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}
		
		if(!isNetworkConntected){	// 네트워크 접속이 되어 있지 않을 때
			Message msg = handler.obtainMessage();
			msg.what = MSG_NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		
		if(disfectionItem != null) {
			this.disfectionProgItem = disfectionItem;
			Message msg = handler.obtainMessage();
			msg.what = MSG_QUARANTINE_COMPLETED;
			handler.sendMessage(msg);
		}
		
	}

	@Override
	public void OnDisinfectionProgramError(ErrorItem errorItem) {
		if(myapp.dialog !=null && myapp.dialog.isShowing()) {
			myapp.dialog.dismiss();
		}

		Message msg = handler.obtainMessage();
		msg.what = MSG_QUARANTINE_ERROR;
		if(errorItem != null) {
			msg.obj = errorItem.getrDesc();
		}
		handler.sendMessage(msg);
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppStatus.navigation.delPathLocation();
	}
	
	private void reesultToast() {
		runOnUiThread(new Runnable() {

            public void run() {
                Toast toast = Toast.makeText(context, mResult, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 50);
                toast.show();
            }
        });
    }
	
}
